<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Sign In</title>

    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/vue@2/dist/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>

    <style>
        html, body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
            background: url("bg.jpg") repeat right center fixed;
            background-size: auto 100%;
            font-family: 'Lato', sans-serif;
        }

        .card {
            max-width: 500px;
            height: 100vh;
            margin: 0 auto;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;

            background-color: rgba(255, 255, 255, 0.25);
            backdrop-filter: blur(4px);
            -webkit-backdrop-filter: blur(4px);
            border: 1px solid rgba(255, 255, 255, 0.18);
            box-shadow: rgba(142, 142, 142, 0.19) 0px 6px 15px 0px;
            -webkit-box-shadow: rgba(142, 142, 142, 0.19) 0px 6px 15px 0px;
            border-radius: 0px;
            -webkit-border-radius: 0px;
            color: rgba(255, 255, 255, 0.75);
        }

        .btns {
            display: flex;
            justify-content: space-around;
        }

        .btn {
            font-size: 17px;
            text-decoration: none;
            padding: .75em 2.5em;
            border-radius: 6em;
            transition: all .2s;
            border: none;
            font-family: inherit;
            background-color: rgba(255, 255, 255, 0.25);
            backdrop-filter: blur(4px);
            margin: 1rem;
            font-weight: 600;
            cursor: pointer;
            text-align: center;
        }

        .list {
            height: 100%;
            width: 100%;
            padding: 1px;
            overflow: auto;
            box-shadow: inset 0 0 30px rgba(255, 255, 255, 0.5);
        }

        .item {
            display: flex;
            justify-content: space-around;
            padding: 10px 20px;
            font-weight: bold;
        }

        .statistics {
            height: 320px;
            width: 100%;
        }

        .del {
            cursor: pointer;
        }

        .el-calendar {
            background: transparent;
        }

        .el-calendar-table .el-calendar-day {
            height: 3rem;
            width: 3rem;
            margin: auto;
        }

        .el-calendar__title {
            color: #fff;
        }

        .el-calendar-table thead th {
            color: #fff;
        }

        .el-calendar-table:not(.is-range) td.next, .el-calendar-table:not(.is-range) td.prev {
            color: rgba(255, 255, 255, 0.4);
        }

        .el-button {
            background: transparent;
            color: #fff;
        }

        .el-calendar-table td.is-selected {
            background-color: rgba(255, 255, 255, 0.3);
        }

        .el-calendar-table .el-calendar-day:hover {
            background-color: rgba(255, 255, 255, 0.3);
        }

        .el-backtop, .el-calendar-table td.is-today {
            font-weight: bold;
        }

        .is-has {
            background-color: rgba(255, 255, 255, 0.2);
            border-radius: 50%;
        }

        .flex-center {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100%;
            height: 100%;
        }
        
        .text-danger {
            color: #F56C6C;
            font-weight: bold;
        }
        
        .day-count {
            font-size: 10px;
            position: absolute;
            bottom: -5px;
            right: -5px;
        }
    </style>
</head>
<body>
<div id="app">
    <div class="container">
        <div class="card">
            <div class="list" v-if="viewMode == 1">
                <div class="item" v-for="(item, index) in data.list" :key="index">
                    {{item}}
                    <div class="del" v-on:click="del(item)">移除</div>
                </div>
            </div>
            <div class="list" v-if="viewMode == 2">
                <div class="item" v-for="(item, index) in data.group_month" :key="index">
                    <div>{{item.month}}</div>
                    <div>{{item.count}}</div>
                </div>
            </div>
            <div class="list" v-if="viewMode == 3">
                <el-calendar>
                    <template slot="dateCell" slot-scope="{date, data}">
                        <div :class="exists(data.day) ? 'is-has flex-center' : 'flex-center'" style="position:relative">
                            {{ data.day.split('-')[2] }}
                            <div class="day-count text-danger" v-if="exists(data.day) > 1">{{ exists(data.day) }}</div>
                        </div>
                    </template>
                </el-calendar>
            </div>

            <div class="statistics" v-on:click="fetchData()">
                <div class="item">总计：{{data.total}}</div>
                <div class="item">近一年：{{data.total_year}}</div>
                <div class="item">近一月：{{data.total_month}}</div>
                <div class="item"><span>近10天：<span v-bind:class="{'text-danger' : data.total_ten >= 8}">{{data.total_ten}}</span></span></div>
                <div class="item">{{data.last}}</div>
            </div>

            <div class="btns">
                <div class="btn month-btn" v-on:click="changeMode()">切换视图</div>
                <div class="btn" v-on:click="add()">增加记录</div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    const request = (params = '') => fetch('sign-in.php?' + params).then(res => res.json())

    new Vue({
        el: '#app',
        data() {
            return {
                viewMode  : 1,
                monthsView: false,
                data      : {}
            }
        },
        mounted() {
            this.fetchData()
        },
        methods: {
            fetchData() {
                request().then(res => {
                    this.data = res

                    if (this.viewMode != 3) {
                        setTimeout(() => {
                            this.goBottom()
                        }, 100)
                    }
                })
            },
            goBottom() {
                document.querySelector('.list').scrollTo({
                    top     : document.querySelector('.list').scrollHeight,
                    behavior: 'smooth'
                })
            },
            add() {
                const confirm = window.confirm('确定要增加记录？')

                if (!confirm) return

                request('m=add').then(() => this.fetchData())
            },
            del(item) {
                const confirm = window.confirm(`确定要移除 ${item} 的记录？`)

                if (!confirm) return

                request(`m=del&item=${item}`).then(() => this.fetchData())
            },
            changeMode() {
                this.viewMode = this.viewMode == 3 ? 1 : this.viewMode + 1
                
                if (this.viewMode != 3) {
                    setTimeout(() => {
                        this.goBottom()
                    }, 100)
                }
            },
            exists(item) {
                return this.data.list.map(i => i.split(' ')[0]).filter(i => i == item)?.length
            }
        }
    })
</script>
</html>
